package com.come4loves.game.server.router;

import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.come4loves.core.comunicate.protocol.Response;
import com.come4loves.core.comunicate.router.Router;
import com.come4loves.core.comunicate.router.RouterHandler;
import com.come4loves.game.server.session.PlayerContext;

/**
 * 模块处理句柄基类,所有接收消息类都必需继承于此
 * 
 * @author ludd
 * 
 */
public abstract class GatewayRouterHandlerImpl extends RouterHandler {

	@Autowired
	protected PlayerContext playerContext;
	@Autowired
	@Qualifier("GatewayRouterImpl")
	private Router router;

	@Override
	public void register() {
		router.register(this);
	}

	@Override
	protected ChannelFutureListener channelFutureListener(final ChannelHandlerContext ctx, final Response response) {
		if (LOGGER.isDebugEnabled()) {
			return new ChannelFutureListener() {
				
				@Override
				public void operationComplete(ChannelFuture arg0) throws Exception {
					
					byte module = response.getModule();
					byte cmd = response.getCmd();
					int byteLength = 0;
					if (response.getValue() != null) {
						byteLength = response.getValue().length;
					}
					Long actorId = playerContext.getActorId(ctx);
					LOGGER.debug(String.format("[messageSent] sessionId:[%s] actorId:[%s] module:[%s] cmd:[%s] statuscode:[%s], length:[%d byte]", playerContext.getAtomicId(ctx), actorId, module, cmd,
							response.getStatusCode(), byteLength));
				}
			};
		}
		return null;
	}
}